package com.itheima.controller.backend;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.dto.SetmealDTO;
import com.itheima.service.SetmealService;
import com.itheima.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐管理")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setMealService;
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 上传图片
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("上传图片")
    public Result<String> upload(MultipartFile imgFile) {
        log.info("上传图片：{}",  imgFile);

        try {
            //1.获取原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            //2.获取文件扩展名
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //3.构造新的文件名
            String objectName = UUID.randomUUID() + extension;
            //4.上传到阿里云,并生成文件请求路径
            String url = aliOssUtil.upload(imgFile.getBytes(), objectName);

            // 保存到redis的一个set集合中
            redisTemplate.opsForSet().add("SETMEAL_IMG", objectName);
            log.info("文件名称：{}",objectName);

            return new  Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, url);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * 分页查询套餐
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    @ApiOperation("分页查询套餐")
    public PageResult PageQuery(@RequestBody QueryPageBean queryPageBean) {
        return setMealService.PageQuery(queryPageBean);
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增套餐")
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO,
                             @RequestParam(name = "checkgroupIds") List<Integer> checkGroupIds) {
        try {
            setMealService.addSetmeal(setmealDTO, checkGroupIds);
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/findById")
    @ApiOperation("根据id查询套餐")
    public Result findById(Integer id) {
        SetmealVO setmealVO = setMealService.findById(id);
        return new Result(true,"success", setmealVO);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping("/edit")
    @ApiOperation("修改套餐")
    public Result update(@RequestBody SetmealDTO setmealDTO,
                         @RequestParam(name = "checkGroupIds") List<Integer> checkGroupIds) {

        try {
            setMealService.update(setmealDTO, checkGroupIds);
            return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }

    /**
     * 删除套餐
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @ApiOperation("删除套餐")
    public Result delete(Integer id) {
        try {
            setMealService.delete(id);
            return new Result(true, "删除成功");
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }

    }

}
